package com.huibur.devicecheck.controller;

import com.huibur.devicecheck.enums.ResultCodeEnum;
import com.huibur.devicecheck.exception.CheckItemNameIsRepeatException;
import com.huibur.devicecheck.model.param.CheckItemParam;
import com.huibur.devicecheck.model.query.CheckItemQuery;
import com.huibur.devicecheck.model.vo.CheckItemVO;
import com.huibur.devicecheck.request.ObjRes;
import com.huibur.devicecheck.request.PageRes;
import com.huibur.devicecheck.service.ICheckItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/*******************************************************
* @title: CheckItemController.java
* @description:
* @author wushiqi
* @date 2021/9/30
* @version 1.0
*******************************************************/
@RestController
@RequestMapping("/checkItems")
@Api(value = "检查项管理",tags = {"检查项管理"})
@Slf4j
public class CheckItemController {

    @Autowired
    ICheckItemService iCheckItemService;

    /**
     * 新增检查项
     * @param checkItemParam
     * @return
     */
    @PostMapping("")
    @ApiOperation(value = "新增检查项")
    public ObjRes<Void> insert(@RequestBody CheckItemParam checkItemParam) {
        try {
            return iCheckItemService.insert(checkItemParam);
        } catch (CheckItemNameIsRepeatException e) {
            log.error(e.getMessage());
            return new ObjRes(ResultCodeEnum.CHECK_ITEM_NAME_IS_REPEAT.getCode(), ResultCodeEnum.CHECK_ITEM_NAME_IS_REPEAT.getLabel());
        }
    }

    /**
     * 删除检查项
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除检查项",response = ObjRes.class)
    public ObjRes<Boolean> deleteById(@PathVariable Long id) {
        return new ObjRes<Boolean>().data(iCheckItemService.deleteById(id));
    }

    /**
     * 编辑检查项
     * @param id
     * @param checkItemParam
     * @return
     */
    @PutMapping("/{id}")
    @ApiOperation(value = "编辑检查项",response = ObjRes.class)
    public ObjRes<Void> updateCheckItem(@PathVariable Long id,@RequestBody CheckItemParam checkItemParam) {
        try {
            return iCheckItemService.updateCheckItem(id,checkItemParam);
        } catch (Exception e) {
            log.error(e.getMessage());
            return new ObjRes(ResultCodeEnum.CHECK_ITEM_NAME_IS_REPEAT.getCode(), ResultCodeEnum.CHECK_ITEM_NAME_IS_REPEAT.getLabel());
        }
    }

    /**
     * 检查项列表(查询并分页)
     * @param checkItemQuery
     * @return
     */
    @GetMapping("")
    @ApiOperation(value = "检查项列表(查询并分页)", response = PageRes.class)
    public PageRes<CheckItemVO> selectTaskList(CheckItemQuery checkItemQuery) {
        return iCheckItemService.selectCheckItemList(checkItemQuery);
    }

    /**
     * 获取检查项详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "获取检查项详情", response = ObjRes.class)
    public ObjRes<CheckItemVO> selectTaskById(@PathVariable Long id) {
        return new ObjRes<CheckItemVO>().data(iCheckItemService.selectCheckItemById(id));
    }

    /**
     * 根据id获取检查项名称
     * @param id
     * @return
     */
    @GetMapping("/selectCheckItemName/{id}")
    @ApiOperation(value = "根据id获取检查项名称", response = ObjRes.class)
    public ObjRes<CheckItemVO> selectCheckItemName(@PathVariable Long id) {
        return new ObjRes<CheckItemVO>().data(iCheckItemService.selectCheckItemName(id));
    }
}
